// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env:'a-1gb4haz213d919e3'
})

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    const db=cloud.database()
    const pics=db.collection('pics')
    const votes=db.collection('votes')
    const $=db.command.aggregate
    
    let res=await pics.get()
    console.log('pics',res)
    let plist=res.data
    res=await votes.aggregate()
                   .group({
                       _id:'$fileid',
                       count:$.sum(1)
                   })
                   .end()
    console.log('votes',res)
    let vlist =res.list

    plist.map(v=>{
        let found =vlist.find(vv=>{
            return vv._id==v.fileid
        })
        if(found){
            v.count=found.count
        }else{
            v.count=0
        }
    })

    return {
        plist
    }
}